﻿using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using Stock.DBUtility;
using Stock.Model;

namespace Stock.Dal
{
    /// <summary>
    /// 数据访问类:StockOptHisDal
    /// </summary>
    public partial class StockOptHisDal
    {
        public StockOptHisDal()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "StockOptHis");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from StockOptHis");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(StockOptHisEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StockOptHis(");
            strSql.Append("ID,UserID,StockID,Price,Copies,Amount,Type,Remark,OptDate)");
            strSql.Append(" values (");
            strSql.Append("@ID,@UserID,@StockID,@Price,@Copies,@Amount,@Type,@Remark,@OptDate)");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,8),
                    new SQLiteParameter("@UserID", DbType.Int32,8),
                    new SQLiteParameter("@StockID", DbType.String),
                    new SQLiteParameter("@Price", DbType.Decimal,4),
                    new SQLiteParameter("@Copies", DbType.Int32,8),
                    new SQLiteParameter("@Amount", DbType.Decimal,4),
                    new SQLiteParameter("@Type", DbType.String),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@OptDate", DbType.String)};
            parameters[0].Value = this.GetMaxId() + 1;
            parameters[1].Value = model.UserID;
            parameters[2].Value = model.StockID;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Copies;
            parameters[5].Value = model.Amount;
            parameters[6].Value = model.Type;
            parameters[7].Value = model.Remark;
            parameters[8].Value = model.OptDate;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(StockOptHisEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StockOptHis set ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("StockID=@StockID,");
            strSql.Append("Price=@Price,");
            strSql.Append("Copies=@Copies,");
            strSql.Append("Amount=@Amount,");
            strSql.Append("Type=@Type,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("OptDate=@OptDate");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@UserID", DbType.Int32,8),
                    new SQLiteParameter("@StockID", DbType.String),
                    new SQLiteParameter("@Price", DbType.Decimal,4),
                    new SQLiteParameter("@Copies", DbType.Int32,8),
                    new SQLiteParameter("@Amount", DbType.Decimal,4),
                    new SQLiteParameter("@Type", DbType.String),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@OptDate", DbType.String),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.StockID;
            parameters[2].Value = model.Price;
            parameters[3].Value = model.Copies;
            parameters[4].Value = model.Amount;
            parameters[5].Value = model.Type;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.OptDate;
            parameters[8].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockOptHis ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockOptHis ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StockOptHisEntity GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,StockID,Price,Copies,Amount,Type,Remark,OptDate from StockOptHis ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = ID;

            StockOptHisEntity model = new StockOptHisEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserID"] != null && ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StockID"] != null && ds.Tables[0].Rows[0]["StockID"].ToString() != "")
                {
                    model.StockID = ds.Tables[0].Rows[0]["StockID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Price"] != null && ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Copies"] != null && ds.Tables[0].Rows[0]["Copies"].ToString() != "")
                {
                    model.Copies = int.Parse(ds.Tables[0].Rows[0]["Copies"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Amount"] != null && ds.Tables[0].Rows[0]["Amount"].ToString() != "")
                {
                    model.Amount = decimal.Parse(ds.Tables[0].Rows[0]["Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["OptDate"] != null && ds.Tables[0].Rows[0]["OptDate"].ToString() != "")
                {
                    model.OptDate = ds.Tables[0].Rows[0]["OptDate"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,StockID,Price,Copies,Amount,Type,Remark,OptDate ");
            strSql.Append(" FROM StockOptHis ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "StockOptHis";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal DataSet GetList(string strwhere, string strAsc)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT ");
            strSql.Append("StockOptHis.ID as ID, ");
            strSql.Append("StockOptHis.UserID as UserID, ");
            strSql.Append("StockOptHis.StockID as StockID, ");
            strSql.Append("StockOptHis.Price as Price , ");
            strSql.Append("StockOptHis.Copies as Copies , ");
            strSql.Append("StockOptHis.Amount as Amount, ");
            strSql.Append("StockOptHis.Type as Type, ");
            strSql.Append("StockOptHis.Remark as Remark, ");
            strSql.Append("StockOptHis.OptDate as OptDate, ");
            strSql.Append("StockInfo.S_Name as StockName ");
            strSql.Append("FROM ");
            strSql.Append("StockOptHis ");
            strSql.Append("left outer JOIN StockInfo ON StockInfo.S_Code = StockOptHis.StockID ");

            if (strwhere.Trim() != "")
            {
                strSql.Append(" where " + strwhere);
            }
            if (strAsc.Trim() != "")
            {
                strSql.Append(" order By  " + strAsc);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        public string GetMinDate()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");

            strSql.Append("Min(OptDate) as minOptDate ");

            strSql.Append("FROM ");
            strSql.Append("StockOptHis ");

            DataSet ds = DbHelperSQLite.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["minOptDate"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        internal decimal getInMoneyList(string p, string p1, string p2)
        {
            decimal backList = 0m;
            decimal prResult;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");

            strSql.Append("Amount  ");

            strSql.Append("FROM ");
            strSql.Append("StockOptHis ");
            strSql.Append("where ");
            if (p2 == "in")
            {
                strSql.Append("Type='银行转存' and  (OptDate<='" + p1 + "' and OptDate>'" + p + "')");
            }
            else
            {
                strSql.Append("Type='银行转出' and  (OptDate<='" + p1 + "' and OptDate>'" + p + "')");
            }

            DataSet ds = DbHelperSQLite.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    decimal.TryParse(dr["Amount"].ToString(), out  prResult);
                    backList += prResult;
                }
                //backList.Add(ds.Tables[0].Rows[0]["Amount"])
            }

            return backList;
        }

        internal decimal getRemainingMoneyList(string p)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT ");

                strSql.Append("sum(Amount)  as SumAmount ");

                strSql.Append("FROM ");
                strSql.Append("StockOptHis ");
                strSql.Append("where ");
                strSql.Append(" OptDate<='" + p + "'");

                DataSet ds = DbHelperSQLite.Query(strSql.ToString());

                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["SumAmount"] != null)
                    {
                        return decimal.Parse(ds.Tables[0].Rows[0]["SumAmount"].ToString());
                    }
                    else
                    {
                        return 0m;
                    }

                    //backList.Add(ds.Tables[0].Rows[0]["Amount"])
                }
                else
                {
                    return 0m;
                }
            }
            catch
            {
                return 0m;
            }
        }

        internal decimal getRemainingCopiesByDate(string stockId, string p)
        {
            decimal backDec = 0m;
            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT ");
            strSql.Append("StockOptHis.ID as ID, ");
            strSql.Append("StockOptHis.UserID as UserID, ");
            strSql.Append("StockOptHis.StockID as StockID, ");
            strSql.Append("StockOptHis.Price as Price , ");
            strSql.Append("StockOptHis.Copies as Copies , ");
            strSql.Append("StockOptHis.Amount as Amount, ");
            strSql.Append("StockOptHis.Type as Type, ");
            strSql.Append("StockOptHis.Remark as Remark, ");
            strSql.Append("StockOptHis.OptDate as OptDate ");
            strSql.Append("FROM ");
            strSql.Append("StockOptHis ");
            strSql.Append("where  ");
            strSql.Append("StockID='" + stockId + "' ");
            strSql.Append(" and OptDate<='" + p + "'");

            DataSet ds = DbHelperSQLite.Query(strSql.ToString());

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["Copies"] != null)
                    {
                        if (dr["Type"].ToString() == "证券买入" || dr["Type"].ToString() == "分红")
                        {
                            backDec += decimal.Parse(dr["Copies"].ToString());
                        }
                        else if (dr["Type"].ToString() == "证券卖出")
                        {
                            backDec -= decimal.Parse(dr["Copies"].ToString());
                        }
                    }
                }
                //backList.Add(ds.Tables[0].Rows[0]["Amount"])
            }

            return backDec;
        }

        internal List<string[]> getStockList(string p)
        {
            List<string[]> backList = new List<string[]>();

            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT distinct StockID from StockOptHis where OptDate<='" + p + "'");

            DataSet ds = DbHelperSQLite.Query(strSql.ToString());
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["StockID"].ToString() != string.Empty)
                {
                    string[] strArray = new string[2];
                    strArray[0] = dr["StockID"].ToString();
                    strArray[1] = getRemainingCopiesByDate(strArray[0], p).ToString();
                    if (strArray[1] != "0")
                    {
                        backList.Add(strArray);
                    }
                }
            }
            return backList;
        }
    }
}